package ggze.mps.home.controller;

import ggze.actuator.ActuatorController;
import ggze.mps.home.dao.inter.UserDao;
import ggze.mps.home.entity.User;
import ggze.tool.BeanToJson;
import ggze.tool.ReturnBody;
import ggze.tool.TimeUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("UserController")
public class UserController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserDao UserDaoName;
    @Autowired
    private ActuatorController actuatorController;

    @RequestMapping("add")
    public String add() throws UnsupportedEncodingException {
        User entity = (User) BeanToJson.jsonTobean(request.getParameter("jon"), User.class);
        ReturnBody returnBody = new ReturnBody();
        entity.setUuid("");
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setCreateTime(time);
            entity.setMethodTime(time);
            actuatorController.execute(entity, "add", UserDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("deleted")
    public String deleted() throws UnsupportedEncodingException {
        User entity = (User) BeanToJson.jsonTobean(request.getParameter("jon"), User.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            UserDaoName.delete("uuid", entity.getUuid());
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("update")
    public String update() throws UnsupportedEncodingException {
        User entity = (User) BeanToJson.jsonTobean(request.getParameter("jon"), User.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setMethodTime(time);
            actuatorController.execute(entity, "update", UserDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("get")
    public String get() throws UnsupportedEncodingException {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            int begin = Integer.parseInt(jsonObject.getString("begin"));
            int end = Integer.parseInt(jsonObject.getString("end"));
            String zdname = jsonObject.getString("zdname");
            String zdval = jsonObject.getString("zdval");
            if ("1".equals(zdval)) {
                List<User> entityLIST = UserDaoName.paging(zdname, zdval, begin, end);
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            } else {
                List<User> entityLIST = UserDaoName.customs("select * from User where " + zdname + "  like '%" + zdval + "%'");
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            }
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("num")
    public String num() throws UnsupportedEncodingException {
        return String.valueOf(UserDaoName.getNum("1", "1"));
    }

    @RequestMapping("seach")
    public String seach(){
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            String stateName=jsonObject.getString("stateName");
            String nickName=jsonObject.getString("nickName");

            String begin = jsonObject.getString("begin");
            String end = jsonObject.getString("end");
            StringBuilder where=new StringBuilder();
            where.append(" where 1=1 ");
            if(!StringUtils.isEmpty(stateName)){
                where.append(" and statename like '%"+stateName+"%'");
            }
            if(!StringUtils.isEmpty(nickName)){
                where.append(" and nickname like '%"+nickName+"%'");
            }

            String sqlCoum="select count(*) from user " + where.toString();
            int coumsNum = UserDaoName.customsNum(sqlCoum);


            where.append(" order by uuid limit "+begin+","+end+"");
            String sqlMain = "select * from user "+ where.toString();


            List<User> users = UserDaoName.customs(sqlMain.toString());

            JSONObject res = new JSONObject();
            res.put("num",coumsNum);
            res.put("data",BeanToJson.toJsonArrayString(users));
            returnBody.setCode("0");
            returnBody.setData(BeanToJson.toJsonObjectString(res));
        }catch (Exception e){
            returnBody.setCode("1");
            returnBody.setTitle("");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }
}
